<?php
/**
 * Created by PhpStorm.
 * User: 跳梁
 * Date: 2017/3/2
 * Time: 10:24
 */
class QualityLifeAction extends Action{
    //获得二维码配置
    public function getCodeConfig(){
        $retVal = Session::init();
        if (!$retVal) {
            Output::instance()->resultCode(code_access_denied);
            return;
        }
        $customerId = Session::$user["customerId"];
        Session::switchCustomer(0);
        $codeConfig = M("vendor_config");
        $supersetting = M("customer_super_setting");
        $vendorModel =M("mivendor");
        Session::switchCustomer($customerId);
        $where["cid"]=array("eq",$customerId);
        $result = $codeConfig->where($where)->find();
        $vendorLimit = $supersetting->where("customerId=$customerId")->getField("vendorLimit");
        $vendorCount = $vendorModel->where("cId=$customerId and deleted=0")->count();
        if($result['qrcodeLocation']){
            $qrcodeLocation = explode("|",$result['qrcodeLocation']);
            $result['codeLocationX'] = $qrcodeLocation[0];
            $result['codeLocationY'] = $qrcodeLocation[1];
        }
        if($result['qrcodeSize']){
            $qrcodeSize = explode("*",$result['qrcodeSize']);
            $result['codeSizeX'] = $qrcodeSize[0];
            $result['codeSizeY'] = $qrcodeSize[1];
        }
        if($result['couponCost']){
            $result['couponCost'] = json_decode($result['couponCost'],true);
        }
        $result['vendorCount']=$vendorCount;
        $result['vendorLimit']=$vendorLimit;
        Output::instance()->object($result);
    }
    //保存配置
    public function saveCodeConfig($qrcodeBack,$qrcodeLocation,$qrcodeSize,$couponCost){
        $retVal = Session::init();
        if (!$retVal) {
            Output::instance()->resultCode(code_access_denied);
            return;
        }
        $customerId = Session::$user["customerId"];
        Session::switchCustomer(0);
        $codeConfig = M("vendor_config");
        Session::switchCustomer($customerId);
        $where["cid"]=array("eq",$customerId);
        $data['qrcodeBack']=$qrcodeBack;
        $data['qrcodeLocation']=$qrcodeLocation;
        $data['qrcodeSize']=$qrcodeSize;
        $data['couponCost']=$couponCost;
        $data['updateTime']=date("Y-m-d H:i:s");
        $result = $codeConfig->where($where)->find();
        if($result){
            $codeConfig->where($where)->save($data);
        }else{
            $data['cid']=$customerId;
            $codeConfig->add($data);
        }
        Output::instance()->resultCode();
    }
    //导出记录
    public function getExportLog($start=0,$limit=10){
        $retVal = Session::init();
        if (!$retVal) {
            Output::instance()->resultCode(code_access_denied);
            return;
        }
        $customerId = Session::$user["customerId"];
        Session::switchCustomer(0);
        $qrcodeExport = M("vendor_qrcode_export");
        Session::switchCustomer($customerId);
        $where["cid"]=array("eq",$customerId);
        $arr = $qrcodeExport->where($where)
            ->limit($start,$limit)->order("createTime desc")
            ->select();
        if($arr){
            $count = $qrcodeExport->where($where)->count();
            Output::instance()->objArray($arr,$count);
        }else{
            Output::instance()->resultCode();
        }

    }
    public function exportCode($count){
        $retVal = Session::init();
        if (!$retVal) {
            Output::instance()->resultCode(code_access_denied);
            return;
        }
        $customerId = Session::$user["customerId"];
        Session::switchCustomer(0);
        $codeConfig = M("vendor_config");
        $codeExport = M("vendor_qrcode_export");
        $storeCode = M("vendor_qrcode");
        Session::switchCustomer($customerId);
        $where["cid"]=array("eq",$customerId);
        $configInfo = $codeConfig->where($where)->find();
        $exportData["cid"]=$customerId;
        $exportData["count"]=$count;
        $exportData["createTime"]=date("Y-m-d H;i:s");
        $exportId = $codeExport->add($exportData);
        $qrcodeData['cid']=$customerId;
        $qrcodeData['exportId']=$exportId;
        $qrcodeData['createTime']=date("Y-m-d H:i:s");
        $path=LOCAL_PIC_PATH . "storeCode/";
        for($i=0;$i<$count;$i++){
            $qrcodeId = $storeCode->add($qrcodeData);
            $data = WeixinApi::createwxaqrcode("page/vendorIndex/vendorIndex?codeId=$qrcodeId","qrcode".date("His").$qrcodeId.".jpg");
            $qrImg = $data->path;
            $storeCode->where("id=$qrcodeId")->setField("qrcodeUrl",$qrImg);
            R("Coupon/createImage",array($path,date("His").$i.$qrcodeId,$qrImg,$configInfo['qrcodeBack'],$configInfo['qrcodeLocation'],$configInfo['qrcodeSize']));
        }
        $result = R("Coupon/getIDZip",array($path,"vendor_code_".time().".zip"));
        $fileUrl = $result->path;
        $codeExport->where("id=$exportId")->setField("fileUrl",$fileUrl);
        $arr['url']=$fileUrl;
        if($fileUrl){
            Output::instance()->object($arr);
        }else{
            Output::instance()->resultCode(1,"导出失败");
        }
    }
}